<?php


namespace app\business\controller;
use app\business\model\SaasOrder;
use think\Db;
use think\Request;

class Order extends Base
{
    // 作废
    //public function index_old(Request $request)
    //{
    //    $crumbs['oneMenuName'] = '商家管理';
    //    $crumbs['twoMenuName'] = '订单列表';
    //
    //    $join = [
    //        [['app_saas_business' => 'b'],'a.b_id=b.id'],
    //        [['app_saas_goods' => 'c'],'a.goods_id=c.id'],
    //        [['app_user' => 'd'],'a.user_id=d.id'],
    //    ];
    //
    //    $search = $request->get('search');
    //    $map = [];
    //    $where = [];
    //    if ($search){
    //        $where['d.nike_name|a.order_code'] = ['LIKE','%'.$search.'%'];
    //        $map['query']['search'] = $search;
    //    }
    //
    //    $data = db('saas_order')->alias('a')
    //        ->field('a.id, GROUP_CONCAT(distinct a.order_code) order_code,
    //         GROUP_CONCAT(c.goods_name) goods_name,
    //         GROUP_CONCAT(b.name) b_name,
    //         d.nike_name user_name, a.addr')
    //        ->join($join)
    //        ->where($where)
    //        ->group('a.order_code')
    //        ->paginate(10, false, $map);
    //    //var_dump($data);
    //    return $this->fetch('', compact('crumbs', 'data'));
    //}

    public function index(Request $request)
    {

        $crumbs['oneMenuName'] = '商家管理';
        $crumbs['twoMenuName'] = '订单列表';

        $join = [
            [['app_saas_business' => 'c'],'a.b_id=c.id'],
            [['app_saas_order' => 'd'],'a.order_code=d.order_code'],
            [['app_user' => 'b'],'d.user_id=b.id'],
        ];

        $search = $request->get('search');
        $map = [];
        $where = [];
        if ($search){
            $where['b.nike_name|a.order_code'] = ['LIKE','%'.$search.'%'];
            $map['query']['search'] = $search;
        }

        $data = db('saas_b_order')->alias('a')
            ->field('a.*, sum(a.goods_number * a.goods_price) as price, b.nike_name user_name, c.name b_name, d.addr, d.pay_type')
            ->join($join)
            ->where($where)
            ->group('a.order_code, a.b_id')
            ->paginate(10, false, $map);
         //var_dump($data);
        return $this->fetch('', compact('crumbs', 'data'));
    }


    public function edit(Request $request)
    {
        if ($request->isPost()){
            $data = $request->post();

            // 状态是已付款则改为已发货
            $status = SaasOrder::get($data['id']);
            if ($status['status'] == 2){
                $data['status'] = 3;
            }

            $res = SaasOrder::update($data);
            if ($res->id){
                $ajax['code'] = 1;
                $ajax['msg'] = '操作成功';
                $ajax['url'] = url('/admin/saasOrder');
            }else{
                $ajax['msg'] = '操作失败';
            }
            return json($ajax);
        }else{
            $crumbs['oneMenuName'] = '商家管理';
            $crumbs['twoMenuName'] = '修改订单';

            $id = input('id');
            $join = [
                [['app_user' => 'b'],'a.user_id=b.id'],
            ];
            $data = db('saas_order')->alias('a')
                ->field('a.*, b.nike_name user_name')
                ->join($join)
                ->find($id);

            return $this->fetch('', compact('crumbs', 'id', 'data'));
        }
    }

    public function details(Request $request)
    {
        $order_code = input('order_code');
        $b_id = input('b_id');
        $where['order_code'] = $order_code;
        $where['b_id'] = $b_id;
        $order_info = db('saas_b_order')->where($where)->select();
        $b_name = db('saas_business')->where('id', $b_id)->value('name');

        foreach($order_info as $k => $v){
            $goods_name = db('saas_goods')->where('id', $v['goods_id'])->value('goods_name');
            $specs_name = db('saas_goods_specs')->where('id', $v['specs_id'])->value('specs_name');

            $data[$k] = [
                'goods_name' => $goods_name,
                'specs_name' => $specs_name,
                'is_group_buying' => $v['is_group_buying'],
                'goods_number' => $v['goods_number'],
                'goods_price' => $v['goods_price'],
            ];
        }
        return $this->fetch('', compact('data', 'b_name'));
    }

    // 废弃
    //public function details_old(Request $request)
    //{
    //    $order_code = input('order_code');
    //    $order_info = db('saas_b_order')->where('order_code', $order_code)->select();
    //
    //    foreach($order_info as $k => $v){
    //        $goods_name = db('saas_goods')->where('id', $v['goods_id'])->value('goods_name');
    //        $specs_name = db('saas_goods_specs')->where('id', $v['specs_id'])->value('specs_name');
    //        $b_name = db('saas_business')->where('id', $v['b_id'])->value('name');
    //
    //        if(!isset($data[$b_name])){
    //            $data[$b_name] = [
    //                'info' => [[
    //                    'goods_name' => $goods_name,
    //                    'specs_name' => $specs_name,
    //                    'b_name' => $b_name,
    //                    'is_group_buying' => $v['is_group_buying'],
    //                    'goods_number' => $v['goods_number'],
    //                    'goods_price' => $v['goods_price'],
    //                ]],
    //            ];
    //        }else{
    //            $data[$b_name]['info'][] = [
    //                'goods_name' => $goods_name,
    //                'specs_name' => $specs_name,
    //                'b_name' => $b_name,
    //                'is_group_buying' => $v['is_group_buying'],
    //                'goods_number' => $v['goods_number'],
    //                'goods_price' => $v['goods_price'],
    //            ];
    //        }
    //    }
    //    return $this->fetch('', compact('data'));
    //}
}